﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using Distrackting.BusinessLayer;
using Distrackting.DataLayer;

namespace Distrackting.Parser
{
    public class Gpx
    {
        List<Track> percursos;

        public Gpx()
        {
            percursos = new List<Track>();
        }

        public void addTrack(Track t) { percursos.Add(t); }

        public void printGpx(String filename, String nometrack)
        {
            try
            {   
                StreamWriter tw = new StreamWriter(filename);
                
                tw.WriteLine("<?xml version=\"1.0\" standalone=\"yes\"?>");
                foreach (Track t in percursos)
                { t.setNome(nometrack);t.printTrack(tw); }
                tw.WriteLine("</gpx>");
                tw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "Cannot open file."); 
                throw e;
            }
           
        }

        public void importarDados()
        {
            foreach (Track t in percursos)
            {
                Percurso p = new Percurso();
                p.cod_percurso = new Guid();
                p.nome = t.getNome();
                List<Ponto> lpontos = new List<Ponto>();
                int ordem = 1;
                foreach (Waypoint w in t.getLista())
                {
                    Ponto pt = new Ponto();
                    pt.cod_percurso = p.cod_percurso;
                    pt.cod_ponto = new Guid();
                    pt.alt = Convert.ToSingle(w.getEle());
                    pt.lat = Convert.ToSingle(w.getLat());
                    pt.lng = Convert.ToSingle(w.getLon());
                    pt.ordem = ordem;
                    ordem++;
                    lpontos.Add(pt);

                }
                dataPercurso.createPercurso(p, lpontos);
            }
        }
    }
}
